﻿using LD.Common;
using System;
using System.Drawing;
using System.Windows.Forms;
using LD.Host;
namespace LD
{
    public partial class FrmMES : FrmBase
    {
        public FrmMES()
        {
            InitializeComponent();
            UserHelper.Instance.UserChangeEvent += Instance_UserChangeEvent;
        }

        private void Instance_UserChangeEvent(UserModel user)
        {
            if (!this.IsDisposed)
            {
                this.Invoke(new Action(() =>
                {
                    if (user.UserType == UserType.操作工)
                        this.panel1.Enabled = false;
                    else
                        this.panel1.Enabled = true;
                }));
            }
        }

        private void btn_get供料器_Click(object sender, EventArgs e)
        {
            if (!string.IsNullOrEmpty(this.txt_sn.Text))
            {
                var responseStr = HttpRequestHelper.Get(this.txt_mesUrl.Text + this.txt_sn.Text);
                if (string.IsNullOrEmpty(responseStr))
                    return;
                if (responseStr.IndexOf("SIZE") >= 0)
                {
                    bool result = false;
                    string[] strs = responseStr.Split(',');
                    //获取最后一个size字符串
                    string sizeStr = strs[strs.Length - 1];
                    if (sizeStr.IndexOf("Space Gray") >= 0)
                    {
                        Cache.SetCache(CONST.A轨_供料器, 2);
                        Cache.SetCache(CONST.B轨_供料器, 2);
                        Cache.SaveCache();

                        result = true;
                    }
                    else
                    {
                        Cache.SetCache(CONST.A轨_供料器, 1);
                        Cache.SetCache(CONST.B轨_供料器, 1);
                        Cache.SaveCache();

                        result = true;
                    }
                    if (!result)
                    {
                        MessageBox.Show("未查询到SIZE关键字后的颜色信息", "警告", MessageBoxButtons.OK, MessageBoxIcon.Error);
                    }
                }
                else
                    MessageBox.Show("返回的字符串未发现SIZE关键字", "警告", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }

        }
        private void btn_save_Click(object sender, EventArgs e)
        {
        
            string oldUrl = Cache.GetCache(CONST.MES_URL).ToString();
            string oldStationName = Cache.GetCache(CONST.MES_STATIONNAME).ToString();
            bool oldMesSw = (bool)Cache.GetCache(CONST.MES_SW);
            string oldImgPath = Cache.GetCache(CONST.MES_IMGPATH).ToString();
            var oldArrayPoint = (string[])Cache.GetCache(CONST.MES_POINTATTR);
            string oldbaliIp = Cache.GetCache(CONST.MES_IP)?.ToString();
            string old_CCDImgPath = Cache.GetCache(CONST.CCD_PATH)?.ToString();

            if (string.IsNullOrEmpty(this.txt_mesUrl.Text)
                || string.IsNullOrEmpty(this.txt_station.Text)
                || string.IsNullOrEmpty(this.txt_imgPath.Text)
                || string.IsNullOrEmpty(this.txt_baliIp.Text)
                || string.IsNullOrEmpty(this.txt_ccdImgPath.Text)
           )
            {
                MessageBox.Show("配置信息不能为空");
                return;
            }
            if (!this.txt_mesUrl.Text.Equals(oldUrl))
            {
                Cache.SetCache(CONST.MES_URL, this.txt_mesUrl.Text);
                Log4.ParamChange($"【MES】,参数【URL】,原值【{oldUrl}】修改为【{this.txt_mesUrl.Text}】");
            }
            if (!this.txt_baliIp.Text.Equals(oldbaliIp))
            {
                Cache.SetCache(CONST.MES_IP, this.txt_baliIp.Text);
                Log4.ParamChange($"【Insight】,参数【BaliIp】,原值【{oldbaliIp}】修改为【{this.txt_baliIp.Text}】");
            }
            if (!this.txt_ccdImgPath.Text.Equals(old_CCDImgPath))
            {
                Cache.SetCache(CONST.CCD_PATH, this.txt_ccdImgPath.Text);
                Log4.ParamChange($"【Insight】,参数【CCD路径】,原值【{old_CCDImgPath}】修改为【{this.txt_ccdImgPath.Text}】");
            }
            if (!this.txt_station.Text.Equals(oldStationName))
            {
                Cache.SetCache(CONST.MES_STATIONNAME, this.txt_station.Text);
                Log4.ParamChange($"【Insight】,参数【StationName】,原值【{oldStationName}】修改为【{this.txt_station.Text}】");
            }
            if (oldMesSw != this.chk_mes_sw.Checked)
            {
                Cache.SetCache(CONST.MES_SW, this.chk_mes_sw.Checked);
                Log4.ParamChange($"【Insight】,参数【开关】,原值【{oldMesSw}】修改为【{this.chk_mes_sw.Checked}】");
            }
            if (!oldImgPath.Equals(this.txt_imgPath.Text))
            {
                Cache.SetCache(CONST.MES_IMGPATH, this.txt_imgPath.Text);
                Log4.ParamChange($"【Insight】,参数【图片路径】,原值【{oldImgPath}】修改为【{this.txt_imgPath.Text}】");
            }
            var array_pointAttr = new string[8];
            for (int i = 0; i < this.dgv_data.Rows.Count; i++)
            {
                array_pointAttr[i] = this.dgv_data[1, i].Value.ToString();
                if (!oldArrayPoint[i].Equals(array_pointAttr[i]))
                {
                    Log4.ParamChange($"【Insight】,参数【点位名称】,原值【{oldArrayPoint[i]}】修改为【{array_pointAttr[i]}】");
                }
            }
            Cache.SetCache(CONST.MES_POINTATTR, array_pointAttr);

            Cache.SaveCache();
        }

        private void btn_bali_Click(object sender, EventArgs e)
        {
            if (MesHelper.Instance.TestStart(new TB_Product() { Barcode = this.txt_msg.Text, EndTime = DateTime.Now }))
                this.lab_msg.Text = "通讯成功";
            else
                this.lab_msg.Text = "通讯失败";
        }

        private void FrmMES_Shown(object sender, EventArgs e)
        {
            this.dgv_data.Rows.Add("螺丝1", "");
            this.dgv_data.Rows.Add("螺丝2", "");
            this.dgv_data.Rows.Add("螺丝3", "");
            this.dgv_data.Rows.Add("螺丝4", "");
            this.dgv_data.Rows.Add("螺丝5", "");
            this.dgv_data.Rows.Add("螺丝6", "");
            this.dgv_data.Rows.Add("螺丝7", "");
            this.dgv_data.Rows.Add("螺丝8", "");

            this.lab_msg.Text = string.Empty;
            this.chk_mes_sw.Checked = (bool)Cache.GetCache(CONST.MES_SW);
            this.txt_mesUrl.Text = Cache.GetCache(CONST.MES_URL).ToString();
            this.txt_imgPath.Text = Cache.GetCache(CONST.MES_IMGPATH).ToString();
            this.txt_station.Text = Cache.GetCache(CONST.MES_STATIONNAME).ToString();
            this.txt_baliIp.Text = Cache.GetCache(CONST.MES_IP)?.ToString();
            this.txt_ccdImgPath.Text = Cache.GetCache(CONST.CCD_PATH)?.ToString();

            var j_pointAttr = Cache.GetCache(CONST.MES_POINTATTR);
            if (j_pointAttr != null)
            {
                var j_pointAttrs = j_pointAttr as string[];
                for (int i = 0; i < j_pointAttrs.Length; i++)
                {
                    this.dgv_data[1, i].Value = j_pointAttrs[i];
                }
            }
        }
    }
}
